/*
 * Copyright 2013 meichenhui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.zet.tool.pluginanalyzer;

import java.io.File;

import org.apache.commons.io.filefilter.AbstractFileFilter;

/**
 * dropins
 * |-- pluginA.jar
 * |-- pluginB*
 *     |-- plugins
 *         |-- pluginsB.jar
 *         |-- pluginC
 *             |-- META-INF*
 *                 |-- MANIFEST.MF
 * |-- pluginD
 *     |-- META-INF*
 *         |-- MANIFEST.MF
 *         
 * @author meichenhui
 *
 */
public class PluginAsDirectoryFilter extends AbstractFileFilter {
	
	private File pluginsDirectory;
	
	public PluginAsDirectoryFilter(File pluginsDirectory) {
	    if (pluginsDirectory == null){
	        throw new RuntimeException("PluginsDirectory must not be null!");
	    }
	    this.pluginsDirectory = pluginsDirectory; 
    }
	
	@Override
	public boolean accept(File file) {
	    if (pluginsDirectory.compareTo(file.getParentFile()) == 0){
	        System.out.println(file);
	        return true;
	    }
	    if (pluginsDirectory.compareTo(file.getParentFile().getParentFile()) == 0 && "plugins".equals(file.getName())){
	        System.out.println(file);
            return true;
        }
        if (pluginsDirectory.compareTo(file.getParentFile().getParentFile()) == 0 && "META-INF".equals(file.getName())){
            System.out.println(file);
            return true;
        }
        if (pluginsDirectory.compareTo(file.getParentFile().getParentFile().getParentFile()) == 0){
            System.out.println(file);
            return true;
        }
        if (pluginsDirectory.compareTo(file.getParentFile().getParentFile().getParentFile().getParentFile()) == 0 && "META-INF".equals(file.getName())){
            System.out.println(file);
            return true;
        }
        System.out.println("false" + file);
	    return false;
	}
	

}
